package com.travel.server.manager.impl.command.project;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.project.QueryProjectEnumRequest;
import com.travel.server.manager.dto.response.project.QueryProjectEnumResponse;
import com.travel.server.manager.dto.response.project.QueryProjectEnumResponse.EnumItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.impl.enums.ProjectEnumTypeEnum;
import com.travel.server.manager.model.dto.ProjectEnumDto;
import com.travel.server.manager.model.dto.SpecialEnumDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 15:32
 * @Desc:
 */
@Component
@Slf4j
public class QueryProjectEnumCommand extends AbstractCommand<QueryProjectEnumRequest, QueryProjectEnumResponse> {

    @Autowired
    private ProjectDal projectDal;

    @Autowired
    private SpecialDal specialDal;

    @Override
    protected void checkInput(QueryProjectEnumRequest request) {
        if(request.getType() == null){
            throwInvalidParam();
        }
        if(request.getType() > 1 && StringUtils.isBlank(request.getProjectType())){
            throwInvalidParam();
        }
        if(request.getType() > 2 && StringUtils.isBlank(request.getProjectType()) && StringUtils.isBlank(request.getCountry())){
            throwInvalidParam();
        }
        if(request.getType() > 3 && StringUtils.isBlank(request.getProjectType()) && StringUtils.isBlank(request.getCountry()) && StringUtils.isBlank(request.getProject())){
            throwInvalidParam();
        }
        if(request.getType() > 4 && StringUtils.isBlank(request.getProjectType()) && StringUtils.isBlank(request.getCountry()) && StringUtils.isBlank(request.getProject())
                && StringUtils.isBlank(request.getProjectName())){
            throwInvalidParam();
        }
    }

    @Override
    public QueryProjectEnumResponse handle(QueryProjectEnumRequest request) {
        QueryProjectEnumResponse response = new QueryProjectEnumResponse();
        List<EnumItem> enumList = new ArrayList<>();

        ProjectEnumDto dto = new ProjectEnumDto();

        Integer type = request.getType();
        if(ProjectEnumTypeEnum.PROJECT_TYPE.getType().equals(type)){
            dto.setValue(ProjectEnumTypeEnum.PROJECT_TYPE.getType());
        }else if(ProjectEnumTypeEnum.COUNTRY.getType().equals(type)){
            dto.setValue(ProjectEnumTypeEnum.COUNTRY.getType());
            dto.setProjectType(request.getProjectType());
        }else if(ProjectEnumTypeEnum.PROJECT.getType().equals(type)){
            dto.setValue(ProjectEnumTypeEnum.PROJECT.getType());
            dto.setProjectType(request.getProjectType());
            dto.setCountry(request.getCountry());
        }else if(ProjectEnumTypeEnum.NAME.getType().equals(type)){
            dto.setValue(ProjectEnumTypeEnum.NAME.getType());
            dto.setProjectType(request.getProjectType());
            dto.setCountry(request.getCountry());
            dto.setProjectGroup(request.getProject());
        }else if(ProjectEnumTypeEnum.TRAVEL_DATE.getType().equals(type)){
            dto.setValue(ProjectEnumTypeEnum.TRAVEL_DATE.getType());
            dto.setProjectType(request.getProjectType());
            dto.setCountry(request.getCountry());
            dto.setProjectGroup(request.getProject());
            dto.setName(request.getProjectName());
        }else{
            throwInvalidParam();
        }

        List<String> list = projectDal.selectEnum(dto);
        if(!CollectionUtil.isEmpty(list)){
            if(!ProjectEnumTypeEnum.TRAVEL_DATE.getType().equals(type)) {
                for (String value : list) {
                    EnumItem item = new EnumItem();
                    item.setName(value);
                    enumList.add(item);
                }
            }else{
                List<Integer> projectIdList = new ArrayList<>();
                for (String value : list) {
                    projectIdList.add(Integer.parseInt(value));
                }
                List<SpecialEnumDto> specialList = specialDal.selectEnum(projectIdList);
                if(!CollectionUtil.isEmpty(specialList)){
                    for(SpecialEnumDto enumDto : specialList){
                        if(enumDto.getTravelDate() != null) {
                            EnumItem item = new EnumItem();
                            item.setName(DateUtil.dateToStr(enumDto.getTravelDate(), DateUtil.YYYY_MM_DD));
                            item.setId(enumDto.getId());
                            enumList.add(item);
                        }
                    }
                }
            }
        }

        response.setEnumList(enumList);
        response.setType(type);
        return response;
    }
}
